/**
 * Created by zqebs on 2016/7/24.
 */

angular.module('Portal').controller('ResetPwdController',
    function ($rootScope, $scope, $state, $cookies, $interval, userInfoService,md5 ,$base64,bcrypt ) {
    $scope.resetPwdInfo = true;

   // $scope.phoneNumberRegex = "((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,2,3,5-9]))\\d{8}";
    $scope.idenCodeButtonContent = "获取手机验证码";
    $scope.isIdenCodeDisable = false;
    $scope.usernameReg = '[a-z0-9]([-a-z0-9]*[a-z0-9])?';
    $rootScope.$broadcast('MainClassSet', 'reg_bg');


    $scope.getIdentifyingCode = function (phoneNumber) {
        userInfoService.getIdentifyingCode(phoneNumber).then(function (response) {
            countDown();
            return response;
        }, function(err) {
            alertify.alert('获取认证码错误', err.data);
        });
    };

    var countDown = function() {
        var timePromise = undefined;
        var second = 60;
        timePromise = $interval(function(){
            if(second <= 0){
                if (timePromise) {
                    $interval.cancel(timePromise);
                    timePromise = undefined;
                    second = 60;
                    $scope.idenCodeButtonContent = "获取手机验证码";
                    $scope.isIdenCodeDisable = false;
                }
            }else{
                $scope.idenCodeButtonContent = second + "秒后可重新获取";
                $scope.isIdenCodeDisable = true;
                second--;
            }
        }, 1000, 100);
    };

    $scope.ok = function () {

       // if($scope.resetPwdForm.$valid){
        if($scope.resetPwdForm.username.$error.unique || ($scope.resetPwdForm.username.$error.unique == undefined && $scope.username != undefined)){
            $scope.resetPwdInfo = false;
            $scope.isResetting = true;

            var userInfo = {
                username: $scope.username,
                email: $scope.email,
                password: md5.createHash($scope.password),
                role: $scope.role,
                company: $scope.company,
                identifyingCode: $scope.identifyingCode,
                phoneNumber: $scope.phoneNumber
            };
            var salt = bcrypt.genSaltSync(10);
            var bHash = bcrypt.hashSync( $scope.password , salt); //generate bCrypt hash
            if( bHash){
                userInfo["bHash"] = bHash ;
                console.log("[DEBUG]>>>>>bHash:"+userInfo.bHash) ;
            }
            //secret in k8s for image pulling
            userInfo['pullSecr'] = $base64.encode($scope.username+':'+$scope.password);

            userInfoService.resetPwd(userInfo).then(function(response){
                $scope.isResetting = false;
                $scope.resetPwdSucceeded = true;
                //window.localStorage.setItem("username", $scope.username)
               // window.localStorage.setItem("hasPhone", "1")
               // $cookies.put("username", $scope.username);
                //$cookies.put("hasPhone", "1");
                window.location.href = "/signin";
                //$state.go("cp",{}, {reload:true});
            }, function(error){
                alertify.alert('create user error', error.data);
            });
        }else{
            alertify.alert('create user error', 'some error in submit form');
        }

    };
});
